package com.jty.module_user.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jty.module_user.R;
import com.jty.module_user.data.DialogData;
import com.jty.module_user.obj.DialogType;

import androidx.recyclerview.widget.RecyclerView;



import java.util.ArrayList;
import java.util.List;

public class DialogAdapter extends RecyclerView.Adapter{
    private List<DialogData> mData= new ArrayList<>();
    int mType;
    public DialogAdapter(List<DialogData> data, int type) {
        mData = data;
        mType = type;
    }

    public List<DialogData> getmData() {
        return mData;
    }

    public List<DialogData> getSelectData() {
        List<DialogData> selectData= new ArrayList<>();
        for(DialogData item : mData) {
            if(item.isSelect()) {
                selectData.add(item);
            }
        }
        return selectData;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == 1) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_dialog_item, parent, false);
            return new HHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof HHolder) {
            ((HHolder)holder).bindData(position);
        }
    }

    @Override
    public int getItemCount() {
        if(mData == null) {
            return 0;
        }
        return mData.size();
    }

    @Override
    public int getItemViewType(int position) {
        return 1;
    }


    class HHolder extends RecyclerView.ViewHolder{

        ImageView iv_select;
        TextView tv_name;

        public HHolder( View itemView) {
            super(itemView);
            iv_select = itemView.findViewById(R.id.iv_select);
            tv_name = itemView.findViewById(R.id.tv_name);
        }

        public void bindData(int position) {
            clickListener(position);

            tv_name.setText(mData.get(position).getName());
//            if(mType == DialogType.INSTANCE.getSUBJECT()) {
//                tv_name.setText(mData.get(position).getSchoolPhaseName() + mData.get(position).getSubjectName());
//            } else {
//                tv_name.setText(mData.get(position).getName());
//            }

//            if(mType == DialogType.INSTANCE.getPHASE() || mType == DialogType.INSTANCE.getSUBJECT()) {
//                iv_select.setVisibility(View.VISIBLE);
//                if(mData.get(position).isSelect()) {
//                    iv_select.setImageResource(R.drawable.ic_check_box_d);
//                } else {
//                    iv_select.setImageResource(R.drawable.ic_check_box_n);
//                }
//            } else {
//                iv_select.setVisibility(View.GONE);
//            }
        }
        private void clickListener(int position) {
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.onItemClick(position, mData.get(position));
                    }
                }
            });
        }
    }

    //第一步 定义接口
    public interface OnItemClickListener {
        void onItemClick(int position, DialogData curData);
    }
    private OnItemClickListener listener;
    //第二步， 写一个公共的方法
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

}
